<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>top_metrics 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-metrics-top-metrics.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-top-metrics.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-top-metrics.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-metrics-top-metrics.html" rel="nofollow" target="_blank">../en/search-aggregations-metrics-top-metrics.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-metrics.html">度量聚合</a></span>
»
<span class="breadcrumb-node">top_metrics 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-metrics-top-hits-aggregation.html">« top_hits 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-valuecount-aggregation.html">value_count 聚合 »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-metrics-top-metrics"></a>最顶端的度量(top_metrics)聚合<a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>
<code class="literal">top_metrics</code> 聚合从文档中选择具有最大或最小 "sort"(排序) 值的度量。

下面的例子将获得文档中具有最大值 <code class="literal">s</code> 的文档的 <code class="literal">m</code> 字段的值：
</p>
<a id="search-aggregations-metrics-top-metrics-simple"></a><div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /test/_bulk?refresh
{"index": {}}
{"s": 1, "m": 3.1415}
{"index": {}}
{"s": 2, "m": 1.0}
{"index": {}}
{"s": 3, "m": 2.71828}
POST /test/_search?filter_path=aggregations
{
  "aggs": {
    "tm": {
      "top_metrics": {
        "metrics": {"field": "m"},
        "sort": {"s": "desc"}
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/401.console"></div>
<p>它将返回:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "aggregations": {
    "tm": {
      "top": [ {"sort": [3], "metrics": {"m": 2.718280076980591 } } ]
    }
  }
}</pre>
</div>
<p>
<code class="literal">top_metrics</code> 在本质上与 <a class="xref" href="search-aggregations-metrics-top-hits-aggregation.html" title="top_hits 聚合"><code class="literal">top_hits</code></a> 非常相似，但是因为对它的限制更多，所以它能够使用更少的内存来完成工作，并且通常更快。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_sort"></a><code class="literal">sort</code>
</h3>
</div></div></div>
<p>
度量请求中的 <code class="literal">sort</code> 字段与 <a class="xref" href="search-request-body.html#request-body-search-sort" title="sort" rel="nofollow">search</a> 请求中的 <code class="literal">sort</code> 字段功能完全相同，除了：

* 它不能用于 <a class="xref" href="binary.html" title="binary 数据类型" rel="nofollow">binary</a>、<a class="xref" href="flattened.html" title="flattened 数据类型" rel="nofollow">flattened</a>、<a class="xref" href="ip.html" title="ip 数据类型" rel="nofollow">ip</a>、<a class="xref" href="keyword.html" title="keyword 数据类型" rel="nofollow">keyword</a> 和 <a class="xref" href="text.html" title="text 数据类型" rel="nofollow">text</a> 字段。

* 它只支持单个 排序(sort) 值，因此当排序值相同时无法区分哪个在前。
</p>
<p>聚合返回的度量是搜索请求返回的第一个命中结果。因此，</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">"sort": {"s": "desc"}</code>
</span>
</dt>
<dd>
从具有最大 <code class="literal">s</code> 值的文档中获取度量
</dd>
<dt>
<span class="term">
<code class="literal">"sort": {"s": "asc"}</code>
</span>
</dt>
<dd>
从具有最小 <code class="literal">s</code> 值的文档中获取度量
</dd>
<dt>
<span class="term">
<code class="literal">"sort": {"_geo_distance": {"location": "35.7796, -78.6382"}}</code>
</span>
</dt>
<dd>
从 <code class="literal">location</code>(位置) <span class="strong strong"><strong>最接近</strong></span> <code class="literal">35.7796, -78.6382</code>的文档中获取度量
</dd>
<dt>
<span class="term">
<code class="literal">"sort": "_score"</code>
</span>
</dt>
<dd>
从相关性评分最高的文档中获取度量
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_metrics"></a><code class="literal">metrics</code>
</h3>
</div></div></div>
<p>
<code class="literal">metrics</code> 选择要从"最顶端(top)"的文档返回的字段。

可以使用类似 <code class="literal">"metric": {"field": "m"}</code> 什么的来请求单个度量，或者通过使用类似 <code class="literal">"metric": [{"field": "m"}, {"field": "i"}</code> 的度量列表来请求多个度量。

下面是一个更完整的例子:
</p>
<a id="search-aggregations-metrics-top-metrics-list-of-metrics"></a><div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test
{
  "mappings": {
    "properties": {
      "d": {"type": "date"}
    }
  }
}
POST /test/_bulk?refresh
{"index": {}}
{"s": 1, "m": 3.1415, "i": 1, "d": "2020-01-01T00:12:12Z"}
{"index": {}}
{"s": 2, "m": 1.0, "i": 6, "d": "2020-01-02T00:12:12Z"}
{"index": {}}
{"s": 3, "m": 2.71828, "i": -12, "d": "2019-12-31T00:12:12Z"}
POST /test/_search?filter_path=aggregations
{
  "aggs": {
    "tm": {
      "top_metrics": {
        "metrics": [
          {"field": "m"},
          {"field": "i"},
          {"field": "d"}
        ],
        "sort": {"s": "desc"}
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/402.console"></div>
<p>它返回:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "aggregations": {
    "tm": {
      "top": [ {
        "sort": [3],
        "metrics": {
          "m": 2.718280076980591,
          "i": -12,
          "d": "2019-12-31T00:12:12.000Z"
        }
      } ]
    }
  }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_size"></a><code class="literal">size</code>
</h3>
</div></div></div>
<p><code class="literal">top_metrics</code> 可以使用 size 参数返回前几个文档的度量值:</p>
<a id="search-aggregations-metrics-top-metrics-size"></a><div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /test/_bulk?refresh
{"index": {}}
{"s": 1, "m": 3.1415}
{"index": {}}
{"s": 2, "m": 1.0}
{"index": {}}
{"s": 3, "m": 2.71828}
POST /test/_search?filter_path=aggregations
{
  "aggs": {
    "tm": {
      "top_metrics": {
        "metrics": {"field": "m"},
        "sort": {"s": "desc"},
        "size": 3
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/403.console"></div>
<p>它将返回:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "aggregations": {
    "tm": {
      "top": [
        {"sort": [3], "metrics": {"m": 2.718280076980591 } },
        {"sort": [2], "metrics": {"m": 1.0 } },
        {"sort": [1], "metrics": {"m": 3.1414999961853027 } }
      ]
    }
  }
}</pre>
</div>
<p>
<code class="literal">size</code> 的默认值是 <code class="literal">1</code>。

size 的最大值默认是<code class="literal">10</code>，因为聚合的工作存储是“密集的”，这意味着我们为每个桶分配 <code class="literal">size</code> 大小的槽。

<code class="literal">10</code> 是一个<span class="strong strong"><strong>非常</strong></span>保守的默认最大值，如果你需要，可以通过更改索引设置 <code class="literal">top_metrics_max_size</code> 来提高该值。

但是你要知道，更大的 size 值会占用相当多的内存，特别是如果它们在一个会生成很多桶的聚合中，比如一个很大的<a class="xref" href="search-aggregations-metrics-top-metrics.html#search-aggregations-metrics-top-metrics-example-terms" title="Use with terms">terms聚合</a>。

如果你仍然想提高它，类似这样操作:
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test/_settings
{
  "top_metrics_max_size": 100
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/404.console"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>如果 <code class="literal">size</code> 大于 <code class="literal">1</code>，<code class="literal">top_metrics</code> 聚合不能用作排序的<span class="strong strong"><strong>目标</strong></span>。</p>
</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_examples_4"></a>示例
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="search-aggregations-metrics-top-metrics-example-terms"></a>和 terms 一起使用
</h4>
</div></div></div>
<p>这种聚合在 <a class="xref" href="search-aggregations-bucket-terms-aggregation.html" title="terms 聚合" rel="nofollow"><code class="literal">terms</code></a>聚合 内部应该非常有用，比如说，要找到每个服务器报告的最后一个值。</p>
<a id="search-aggregations-metrics-top-metrics-terms"></a><div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /node
{
  "mappings": {
    "properties": {
      "ip": {"type": "ip"},
      "date": {"type": "date"}
    }
  }
}
POST /node/_bulk?refresh
{"index": {}}
{"ip": "192.168.0.1", "date": "2020-01-01T01:01:01", "m": 1}
{"index": {}}
{"ip": "192.168.0.1", "date": "2020-01-01T02:01:01", "m": 2}
{"index": {}}
{"ip": "192.168.0.2", "date": "2020-01-01T02:01:01", "m": 3}
POST /node/_search?filter_path=aggregations
{
  "aggs": {
    "ip": {
      "terms": {
        "field": "ip"
      },
      "aggs": {
        "tm": {
          "top_metrics": {
            "metrics": {"field": "m"},
            "sort": {"date": "desc"}
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/405.console"></div>
<p>它返回:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "aggregations": {
    "ip": {
      "buckets": [
        {
          "key": "192.168.0.1",
          "doc_count": 2,
          "tm": {
            "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 2 } } ]
          }
        },
        {
          "key": "192.168.0.2",
          "doc_count": 1,
          "tm": {
            "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 3 } } ]
          }
        }
      ],
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0
    }
  }
}</pre>
</div>
<p>与 <code class="literal">top_hits</code> 不同的是，可以根据此度量的结果对桶进行排序:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /node/_search?filter_path=aggregations
{
  "aggs": {
    "ip": {
      "terms": {
        "field": "ip",
        "order": {"tm.m": "desc"}
      },
      "aggs": {
        "tm": {
          "top_metrics": {
            "metrics": {"field": "m"},
            "sort": {"date": "desc"}
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/406.console"></div>
<p>它返回:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "aggregations": {
    "ip": {
      "buckets": [
        {
          "key": "192.168.0.2",
          "doc_count": 1,
          "tm": {
            "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 3 } } ]
          }
        },
        {
          "key": "192.168.0.1",
          "doc_count": 2,
          "tm": {
            "top": [ {"sort": ["2020-01-01T02:01:01.000Z"], "metrics": {"m": 2 } } ]
          }
        }
      ],
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0
    }
  }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="_mixed_sort_types"></a>混合的排序类型
</h4>
</div></div></div>
<p>按不同索引中具有不同类型的字段对 <code class="literal">top_metrics</code> 进行排序会产生一些意想不到的结果：浮点型字段总是独立于整型字段进行排序。</p>
<a id="search-aggregations-metrics-top-metrics-mixed-sort"></a><div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">
# 插入的文档的m字段的值有浮点型，也有整型
POST /test/_bulk?refresh
{"index": {"_index": "test1"}}
{"s": 1, "m": 3.1415}
{"index": {"_index": "test1"}}
{"s": 2, "m": 1}
{"index": {"_index": "test2"}}
{"s": 3.1, "m": 2.71828}

#搜索
POST /test*/_search?filter_path=aggregations
{
  "aggs": {
    "tm": {
      "top_metrics": {
        "metrics": {"field": "m"},
        "sort": {"s": "asc"}
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/407.console"></div>
<p>它返回：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "aggregations": {
    "tm": {
      "top": [ {"sort": [3.0999999046325684], "metrics": {"m": 2.718280076980591 } } ]
    }
  }
}</pre>
</div>
<p>
虽然这比返回一个错误要好，但它<span class="strong strong"><strong>可能</strong></span>不是你想要的。

虽然它确实损失了一些精度，但是你可以使用类似下面的代码将整个数值字段显式地转换为浮点型:
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /test*/_search?filter_path=aggregations
{
  "aggs": {
    "tm": {
      "top_metrics": {
        "metrics": {"field": "m"},
        "sort": {"s": {"order": "asc", "numeric_type": "double"}}
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/408.console"></div>
<p>它返回的数据与我们期望的更接近：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "aggregations": {
    "tm": {
      "top": [ {"sort": [1.0], "metrics": {"m": 3.1414999961853027 } } ]
    }
  }
}</pre>
</div>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-metrics-top-hits-aggregation.html">« top_hits 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-valuecount-aggregation.html">value_count 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>